/**
 * Created by xc on 2016/05/12.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var EvntPptVoteModel = function(obj){
    ItbBaseModel.call(this,obj);

};
EvntPptVoteModel.prototype = new ItbBaseModel();

// 取得表名
EvntPptVoteModel.prototype.getTableName = function(){
    return 'evnt_ppt_vote';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
EvntPptVoteModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'id='+this.id;
}

//------------------------------------------------------------------
/* 获取各PPT投票数
 */
EvntPptVoteModel.prototype.getPPTVoteCnt = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('EvntPptVoteModel.getPPTVoteCnt Err:'+err);
        }

        // Use the connection
        var sql = ' SELECT t.ppt_id, count(*) AS cnt FROM evnt_ppt_vote t WHERE t.del_flg = 0 '+
            ' GROUP BY t.ppt_id ORDER BY t.ppt_id';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

//------------------------------------------------------------------
/* 获取用户当日投票数
 */
EvntPptVoteModel.prototype.getUserTodayVoteCnt = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('EvntPptVoteModel.getUserTodayVoteCnt Err:'+err);
        }

        // Use the connection
        var sql = ' SELECT t.user_id, count(*) AS cnt FROM evnt_ppt_vote t '+
            ' WHERE t.del_flg = 0 AND t.update_time >= CURDATE() AND t.update_time < DATE_ADD(CURDATE(),INTERVAL 24 HOUR) ';
        if(THIS.user_id) {
            sql += ' AND t.user_id= ' + THIS.user_id;
        }
        sql += ' GROUP BY t.user_id ORDER BY t.user_id';

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

//------------------------------------------------------------------
/* 获取用户当日投票数
 */
EvntPptVoteModel.prototype.chkUserTodayVote = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('EvntPptVoteModel.chkUserTodayVote Err:'+err);
        }

        // Use the connection
        var sql = ' SELECT count(*) AS cnt FROM evnt_ppt_vote t '+
            ' WHERE t.del_flg = 0 AND t.update_time >= CURDATE() AND t.update_time < DATE_ADD(CURDATE(),INTERVAL 24 HOUR) '+
            ' AND t.user_id= ' + THIS.user_id +
            ' AND t.ppt_id= ' + THIS.ppt_id;

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            if (result && result.length > 0) {
                return callback(error, result[0].cnt);
            } else {
                return callback(error, 0);
            }
        });
    });
}

//------------------------------------------------------------------
/* 获取院校投票情况
 */
EvntPptVoteModel.prototype.getCampusVoteCnt = function(callback, skipCnt, pageSize){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('EvntPptVoteModel.getCampusVoteCnt Err:'+err);
        }

        // Use the connection
        var sql = ' SELECT v.id, c.name, v.cnt '+
            ' FROM '+
            ' (SELECT t.campus AS id, count(*) AS cnt '+
            '  FROM evnt_ppt_vote t '+
            '  WHERE t.del_flg = 0 GROUP BY t.campus) v,'+
            ' tg_campus c '+
            ' WHERE v.id=c.id AND c.del_flg=0 '+
            ' ORDER BY v.cnt DESC ';

        if (typeof skipCnt!='undefined' && skipCnt != null && typeof pageSize!='undefined' && pageSize != null){
            sql += " limit " + Number(skipCnt) + "," + Number(pageSize);
        }

        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            console.log();

            callback(error, result);
        });
    });
}

// 导出
module.exports = EvntPptVoteModel;